package de.hdu.pms.ctrl;

import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import de.hdu.pms.dao.MitarbeiterDao;
import de.hdu.pms.dao.UrlaubDao;
import de.hdu.pms.model.Mitarbeiter;
import de.hdu.pms.model.Urlaub;

@Controller
public class UrlaubController {
	private static Logger Log = Logger.getLogger(UrlaubController.class);
	private UrlaubDao urlaubDao = null;
	private MitarbeiterDao mitarbeiterDao = null;
	private Urlaub urlaub = null;

	public void setUrlaubDao(UrlaubDao urlaubDao) {
		this.urlaubDao = urlaubDao;
	}

	public MitarbeiterDao getMitarbeiterDao() {
		return mitarbeiterDao;
	}

	public void setMitarbeiterDao(MitarbeiterDao mitarbeiterDao) {
		this.mitarbeiterDao = mitarbeiterDao;
	}

	/**
	 * <p>
	 * Edits Urlaub.
	 * </p>
	 * <p>
	 * Expected HTTP GET and request '/EditUrlaub.html'.
	 * </p>
	 * <p>
	 * Linked Viewname to 'edit-urlaub.jsp'
	 * 
	 * @return mv
	 * @author BBV
	 **/
	@RequestMapping(value = "/EditUrlaub.html", method = RequestMethod.GET)
	public ModelAndView edit(HttpServletRequest request, Integer id) {
		Log.debug("EditUrlaub.html");
		ModelAndView mv = new ModelAndView();
		if (id == null) {
			Log.info("ID not Found!");
			HttpSession session = request.getSession();
			Integer mit_id = Integer.parseInt(session.getAttribute("user")
					.toString());
			mv.addObject(new Urlaub());
			mv.addObject("mitarbeiter", mitarbeiterDao.findById(mit_id));
			mv.addObject("check", true);
		} else {
			Log.info("ID Found!");
			mv.addObject(urlaubDao.findById(id));
			mv.addObject("mitarbeiter", mitarbeiterDao.findById(id));
			mv.addObject("check", false);
		}
		mv.addObject("title", "Urlaubsantrag");
		mv.addObject("message", "Urlaubsantrag bearbeiten");
		mv.setViewName("edit-urlaub");
		Log.info("Edit-Urlaub Return mv!");
		return mv;
	}

	/**
	 * <p>
	 * Save Urlaub
	 * </p>
	 * <p>
	 * Expected HTTP Post and request '/SaveUrlaub.html'
	 * <p>
	 * redirects to '/Urlaub.html'
	 * 
	 * @param urlaub
	 * @param model
	 * @return
	 * @author BBV
	 */
	@RequestMapping(value = "/SaveUrlaub.html", method = RequestMethod.POST)
	public String save(HttpServletRequest request, Model model, Integer m_id,
			Integer[] id, String urlaub_von, String urlaub_bis, boolean admin) {

		String[] u_von = urlaub_von.split("\\.");
		String[] u_bis = urlaub_bis.split("\\.");
		this.urlaub = new Urlaub();
		if (id != null) {
			this.urlaub.setId(id[0]);
		}
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, Integer.parseInt(u_von[0]));
		calendar.set(Calendar.MONTH, Integer.parseInt(u_von[1]) - 1);
		calendar.set(Calendar.YEAR, Integer.parseInt(u_von[2]));
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		this.urlaub.setUrlaub_von(calendar.getTime());
		calendar.set(Calendar.DATE, Integer.parseInt(u_bis[0]));
		calendar.set(Calendar.MONTH, Integer.parseInt(u_bis[1]) - 1);
		calendar.set(Calendar.YEAR, Integer.parseInt(u_bis[2]));
		calendar.set(Calendar.HOUR_OF_DAY, 00);
		calendar.set(Calendar.MINUTE, 00);
		calendar.set(Calendar.SECOND, 00);
		this.urlaub.setUrlaub_bis(calendar.getTime());
		urlaubDao.save(urlaub, m_id);
		model.addAttribute("statusMessageKey", "projekte.form.msg.success");
		HttpSession session = request.getSession();
		boolean admincheck = (boolean) session.getAttribute("admin");
		if (admincheck == true && admin == true) {
			return "redirect:Urlaub_Admin.html";
		} else {
			return "redirect:Urlaub.html";
		}

	}

	/**
	 * <p>
	 * Lists Urlaub
	 * </p>
	 * <p>
	 * Expected HTTP GET and request for '/Urlaub.html'
	 * <p>
	 * Linked Viewname to 'list-urlaub.jsp'
	 * 
	 * @return mv
	 * @author BBV
	 */
	@RequestMapping(value = "/Urlaub.html", method = RequestMethod.GET)
	public ModelAndView findAll(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Integer mit_id = Integer.parseInt(session.getAttribute("user")
				.toString());
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Urlaub");
		mv.addObject("message", "Verwaltung der Urlaubsanträge");
		mv.addObject("urlaub", urlaubDao.findByMitarbeiter(mit_id));
		mv.setViewName("list-urlaub");
		return mv;
	}

	/**
	 * <p>
	 * Lists Urlaub Admin
	 * </p>
	 * <p>
	 * Expected HTTP GET and request for '/Urlaub_Admin.html'
	 * <p>
	 * Linked Viewname to 'list-urlaub_admin.jsp'
	 * 
	 * @return mv
	 * @author BBV
	 */
	@RequestMapping(value = "/Urlaub_Admin.html", method = RequestMethod.GET)
	public ModelAndView findAll_admin() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Urlaub");
		mv.addObject("message", "Verwaltung der Urlaubsantraege");
		mv.addObject("urlaub", urlaubDao.findUnchecked());
		mv.addObject("messageAll", "Uebersicht aller Urlaubsantraege");
		mv.addObject("all", urlaubDao.findAll());
		mv.setViewName("list-urlaub_admin");
		return mv;
	}

	/**
	 * <p>
	 * Permits a Urlaubsantrag
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP POST and request '/Urlaubgenehmigen.html'.
	 * </p>
	 * 
	 * @return
	 */
	@RequestMapping(value = "/Urlaubgenehmigen.html", method = RequestMethod.GET)
	public String permit(@RequestParam(required = true) Integer id) {
		urlaubDao.permit(id);
		return "redirect:Urlaub_Admin.html";
	}

	/**
	 * <p>
	 * Defeats a Urlaubsantrag
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP POST and request '/Urlaubablehnen.html'.
	 * </p>
	 * 
	 * @return
	 */
	@RequestMapping(value = "/Urlaubablehnen.html", method = RequestMethod.GET)
	public String defeat(@RequestParam(required = true) Integer id) {
		urlaubDao.defeat(id);
		return "redirect:Urlaub_Admin.html";
	}

	/**
	 * <p>
	 * Deletes a Urlaubsantrag
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP POST and request '/DeleteUrlaub.html?id=5'.
	 * </p>
	 * 
	 * @return
	 */
	@RequestMapping(value = "/DeleteUrlaub.html", method = RequestMethod.GET)
	public String delete(Urlaub urlaub, Integer m_id, boolean admin) {
		Mitarbeiter mit = new Mitarbeiter();
		mit = mitarbeiterDao.findById(m_id);
		urlaub.setMitarbeiter(mit);
		urlaubDao.delete(urlaub);
		if (admin == true) {
			return "redirect:Urlaub_Admin.html";
		} else {
			return "redirect:Urlaub.html";
		}
	}
}